<template>
	<om-formsub-outer :index="index" :checkedit="checkedit" :deleteBtn="isShowDel">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		hasEditor,
		changeEditor,
		configSelectData,
		hasValue,
		getFormFields
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr
	} from '@/static/js/common.js';
	import urlUtil from "@/static/js/urlUtil.js"
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				isShowDel:false,
				indexCacheKey: "obj-month-tmonthlyPlan-index-index-cache",
				user1CacheKey: "obj-month-tmonthlyPlan-index-user1-cache",
				exists:"",
				
			})
		},
		methods: {
			getCategoryList(indexId){
				let _that = this
				_that.om.request(
					"indexCategory/findIndexCategoryList",{
						indexDictId:indexId
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title_index", 'rangeId', 'type', 'selectbox')
					_that.$refs.autoform.changeEditor("title_index","rangeId","data",configSelectData(res,"indexCategoryId", "indexCategoryName"))
					_that.$refs.autoform.initSelectIndex("title_index","rangeId")
				})
			},
			inputWeight(val){
				let _that = this
				if(Number(val) > 100){
					_that.$refs.autoform.formdata.weight = 100
				}else if(Number(val) <1){
					_that.$refs.autoform.formdata.weight = 1
				}
			},
			onSelectCheckFlag() {
				let _that = this
				_that.$refs.autoform.changeEditor("title_index", "weight","required",true)
				_that.$refs.autoform.changeEditor("title_index", "weight", "type", "numberbox")
			},
			clearChecksId(){
				let _that = this
				_that.$refs.autoform.clear("title_index","checkFlagCode")
				_that.$refs.autoform.clear("title_index","weight")
				_that.$refs.autoform.changeEditor("title_index", "weight", "type", "labelbox")
				_that.$refs.autoform.changeEditor("title_index", "weight","required",false)
			},
			getCheckList() {
				let _that = this
				_that.om.request(
					"dictZero/getListByCode", {
						code: "36924",
						inCode: "36925"
					}
				).then(res => {
					if(uni.getStorageSync('objIndexList') == 'indexList'){
						_that.$refs.autoform.changeEditor("title_index", 'checkFlagCode', 'type', 'labelbox')
					}else{
						_that.$refs.autoform.changeEditor("title_index", 'checkFlagCode', 'type', 'selectbox')
						_that.$refs.autoform.changeEditor("title_index","checkFlagCode","data",configSelectData(res,"code","dicname"))
						_that.$refs.autoform.changeEditor("title_index", "checkFlagCode", "onSelect", "onSelectCheckFlag")
						_that.$refs.autoform.initSelectIndex("title_index", "checkFlagCode")
					}
					
				})
			},
			getOtherList(){
				let _that = this
				_that.$nextTick(function(){
					_that.$refs.autoform.changeEditor("title_index", "targetValueCode","required",true)
					_that.$refs.autoform.changeEditor("title_index", "targetValueCode", "type", "numberbox")
					_that.$refs.autoform.changeEditor("title_index", "challengeTargetCode", "type", "numberbox")
					_that.$refs.autoform.changeEditor("title_index", "remark", "type", "textarea")
					_that.$refs.autoform.changeEditor("title_index", "dutyOffId", "type", "linkbox")
					_that.$refs.autoform.changeEditor("title_index", "dutyOffId","required",true)
				})
				
			}
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key: _that.indexCacheKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							_that.$refs.autoform.clear("title_index", "checkFlweightagCode")
							_that.$refs.autoform.clear("title_index", "weight")
							_that.initValue.weight = ""
							formdata.indexName = d.dicname
							formdata.indexId = d.id
							_that.getCategoryList(d.id)
							formdata.rangeId = d.rangeId
							formdata.busRange = d.busRange
							formdata.annualTargetName = d.targetValueName
							_that.getOtherList()
							_that.$refs.autoform.changeEditor("title_index", "targetValueCode", "prefix", d.logicRelationshipName)
							_that.$refs.autoform.changeEditor("title_index", "targetValueCode", "suffix", d.indexUnitName)
							_that.$refs.autoform.changeEditor("title_index", "challengeTargetCode", "prefix", d.logicRelationshipName)
							_that.$refs.autoform.changeEditor("title_index", "challengeTargetCode", "suffix", d.indexUnitName)
							formdata.prefix = d.logicRelationshipName
							formdata.suffix = d.indexUnitName
							_that.getCheckList()
							uni.removeStorage({
								key: _that.indexCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key: _that.user1CacheKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.dutyOffName = d.userName
							formdata.dutyOffId = d.id
							formdata.dutyPosId = d.posId
							uni.removeStorage({
								key: _that.user1CacheKey
							})
						}
					}
				})

			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if (e.configDataCacheKey) {
					_that.exists = _that.datas.filter((e,i)=>{
						return (i != _that.index)
					}).map(e=>{
						if(!e.rangeId){
							return e.indexId
						}else{
							return null
						}
					}).filter((e)=>{
						return (e !== "" && e !== null)
					}).join(",")
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							if (res.data && Object.prototype.toString.call(res.data) === '[object Array]') {
								_that.autoformdata = res.data
								
								let readonlyVal = false;
								if(!_that.index && _that.index != 0){
									_that.formdata.deleteInMonthProj = true
								}
								if (_that.formdata.autoFlag) {
								    if (!_that.formdata.deleteInMonthProj) {
								        readonlyVal = true;
								    }
								}
								// if(_that.datas[(Number(_that.order) - 1)] !== undefined && _that.datas[(Number(_that.order) - 1)].deleteInMonthProj !== ""){
								// 	_that.isShowDel =  _that.datas[(Number(_that.order) - 1)].deleteInMonthProj
								// 	_that.formdata.deleteInMonthProj =  _that.datas[(Number(_that.order) - 1)].deleteInMonthProj
								// }
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								if (_that.datas[(Number(_that.order) - 1)] && _that.datas[(Number(_that.order) - 1)].indexId) {
									if(!_that.formdata.kpiFlag && !_that.formdata.upperFlag){
										_that.getCheckList()
										_that.getOtherList()
										changeEditor("title_index", "targetValueCode", "prefix", _that.datas[(Number(_that.order) - 1)].logicRelationshipName,
											_that.autoformdata)
										changeEditor("title_index", "targetValueCode", "suffix", _that.datas[(Number(_that.order) - 1)].indexUnitName,
											_that.autoformdata)
										changeEditor("title_index", "challengeTargetCode", "prefix", _that.datas[(Number(_that.order) - 1)].logicRelationshipName,
											_that.autoformdata)
										changeEditor("title_index", "challengeTargetCode", "suffix", _that.datas[(Number(_that.order) - 1)].indexUnitName,
											_that.autoformdata)
									}
								}
								let objFormCode = JSON.parse(uni.getStorageSync('objFormCode'))
								changeEditor("title_index", "orderNum",{
									defaultName: objFormCode.DICT_CODE_R + _that.order
								}, null, _that.autoformdata)
								if(!readonlyVal){
									changeEditor("title_index", "indexId", {
										link: "../../public/pageSearch/pageSearch",
										"linkParam": {
											other: JSON.stringify({
												// lList:_that.exists,
												// instId:Number(e.instId)
											}),
											namefield: "dicname",
											notefield: "indexUnitName",
											extrafield: "logicRelationshipName",
											cachename: _that.indexCacheKey,
											url:'indexDict/getAllIndex2',
											searchfield: "q",
											initload: true,
										}
									}, null, _that.autoformdata)
								}else{
									changeEditor("title_index", "indexId", 'type','labelbox',_that.autoformdata)
								}
								
								changeEditor("title_index","dutyOffId", {
									link: "../../public/pageSearch/pageSearch",
									"linkParam":{
										other: JSON.stringify({
											userId:e.userId
										}),
										namefield: urlUtil.userPageUrl().namefield,
										searchfield: "q",
										initload: true,
										url: "orgInstPosUserOfficial/findSelfListByUserIdOrgId",
										cachename: _that.user1CacheKey,
										placeholder: "请输入姓名或岗位"
									}
								}, null,_that.autoformdata)
								if(e.edit ==="true"){
									if(_that.formdata.kpiFlag){
										_that.isShowDel = false
										let arr = getFormFields(_that.autoformdata)
										for(let index in arr){
											changeEditor("title_index",arr[index],'type','labelbox',_that.autoformdata)
										}
										setTimeout(()=>{
											_that.om.toast('当前指标为kpi配置指标，仅做显示，禁止修改',2000)
										},500)
									}else{
										_that.getCategoryList(_that.formdata.indexId)
										_that.getCheckList()
										_that.getOtherList()
									}
								}else if(_that.formdata.indexId){
									if(_that.formdata.kpiFlag){
										_that.isShowDel = false
										let arr = getFormFields(_that.autoformdata)
										for(let index in arr){
											changeEditor("title_index",arr[index],'type','labelbox',_that.autoformdata)
										}
										setTimeout(()=>{
											_that.om.toast('当前指标为kpi配置指标，仅做显示，禁止修改',2000)
										},500)
									}else{
										_that.getCategoryList(_that.formdata.indexId)
										_that.getCheckList()
										_that.getOtherList()
									}
								}
								
							}
							_that.$nextTick(function(){
								if (_that.$refs.autoform.formdata.checkFlagCode) {
									_that.$refs.autoform.changeEditor("title_index", "weight", "type", "numberbox")
								}
							})
						}
					})
				}

			})
		}
	}
</script>

<style>
</style>
